﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.DBModule;

namespace ZX.BLL
{
    public class BsalSalarySolution : BaseBussnies
    {
        public override bool Insert(object entitys, out string msg)
        {
            try
            {
                bool bl = base.Insert(entitys, out msg);
                if (bl)
                {
                    //更新该薪资方案的薪资项目
                    salSalarySolution salarySolution = entitys as salSalarySolution;
                    if (salarySolution.ItemId.Trim() != string.Empty)
                    {
                        string[] token = salarySolution.ItemId.Trim().Split(',');
                        foreach (string s in token)
                        {
                            if (s.Trim() == string.Empty) continue;
                            Guid guid = Guid.Empty;
                            try
                            {
                                guid = Guid.Parse(s.Trim());
                            }
                            catch { }
                            if (guid == Guid.Empty) continue;
                            //获取原始薪资项目
                            salSalaryItem initItem = new salSalaryItem();
                            BaseBussnies bll = new BaseBussnies(initItem);
                            initItem = bll.GetEntityByCondition("ID='" + s.Trim() + "'") as salSalaryItem;
                            //创建对应该方案的薪资项
                            salSalaryItem item = initItem.Clone() as salSalaryItem;
                            item.ID = Guid.NewGuid();
                            item.SalarySolutionID = salarySolution.ID;
                            bll = new BaseBussnies(item);
                            bll.Insert();
                        }
                    }
                }
                return bl;
            }
            catch (Exception ex)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    BaseBussnies errBll = new BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = this.GetType().Module.Name;
                    salaryErrLog.className = this.GetType().Name;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = ex.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
                msg = ex.Message;
                return false;
            }
        }

        public override bool Update(object entitys, out string msg)
        {
            try
            {
                bool bl = base.Update(entitys, out msg);
                if (bl)
                {
                    salSalarySolution salarySolution = entitys as salSalarySolution;
                    //先删除之前对应的薪资项
                    salSalaryItem delItem = new salSalaryItem();
                    BaseBussnies delBll = new BaseBussnies(delItem);
                    delBll.delete("SalarySolutionID='" + salarySolution.ID + "'");
                    //更新该薪资方案的薪资项目
                    if (salarySolution.ItemId.Trim() != string.Empty)
                    {
                        string[] token = salarySolution.ItemId.Trim().Split(',');
                        foreach (string s in token)
                        {
                            if (s.Trim() == string.Empty) continue;
                            Guid guid = Guid.Empty;
                            try
                            {
                                guid = Guid.Parse(s.Trim());
                            }
                            catch { }
                            if (guid == Guid.Empty) continue;
                            //获取原始薪资项目
                            salSalaryItem initItem = new salSalaryItem();
                            BaseBussnies bll = new BaseBussnies(initItem);
                            initItem = bll.GetEntityByCondition("ID='" + s.Trim() + "'") as salSalaryItem;
                            if (initItem != null)
                            {
                                //创建对应该方案的薪资项
                                salSalaryItem item = initItem.Clone() as salSalaryItem;
                                item.ID = Guid.NewGuid();
                                item.SalarySolutionID = salarySolution.ID;
                                bll = new BaseBussnies(item);
                                bll.Insert();
                            }
                        }
                    }
                }
                return bl;
            }
            catch (Exception ex)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    BaseBussnies errBll = new BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = this.GetType().Module.Name;
                    salaryErrLog.className = this.GetType().Name;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = ex.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
                msg = ex.Message;
                return false;
            }
        }
    }
}
